package nextel.ruta.bean;

import pe.com.nextel.rms.DataInputStreamX;
import pe.com.nextel.rms.DataOutputStreamX;
import pe.com.nextel.rms.PersistenteX;
import java.io.IOException;
import pe.com.nextel.util.ArrayList;
import pe.com.nextel.util.Comparable;
import pe.com.nextel.util.Iterator;
import pe.com.nextel.util.List;
import pe.com.nextel.util.StringUtils;
import org.kxml2.io.KXmlParser;
import org.kxml2.io.KXmlSerializer;
/**
 *
 * @author eespinoza
 */
public class RutaPorCodigoBean extends PersistenteX implements Comparable {

    private int idRecordRuta;
    private short posRecordRuta;
    private String campoBuscar;

    public void persistir(DataOutputStreamX dataOutput, List list) throws IOException {
        dataOutput.writeInt(list.size());
        for (Iterator it = list.iterator(); it.hasNext();) {
            RutaPorCodigoBean clienteCodigo = (RutaPorCodigoBean) it.next();
            dataOutput.writeInt(clienteCodigo.getIdRecordRuta());
            dataOutput.writeShort(clienteCodigo.getPosRecordRuta());
            dataOutput.writeString(StringUtils.stringNullToBlank(clienteCodigo.getCampoBuscar()));
        }
    }

    public List recuperar(DataInputStreamX dataInput) throws IOException {
        List lista = new ArrayList();
        RutaPorCodigoBean clienteCodigo = null;
        int cant = dataInput.readInt();
        for (int i = 0; i < cant; i++) {
            clienteCodigo = new RutaPorCodigoBean();
            clienteCodigo.setIdRecordRuta(dataInput.readInt());
            clienteCodigo.setPosRecordRuta(dataInput.readShort());
            clienteCodigo.setCodigoRuta(dataInput.readString());
            lista.add(clienteCodigo);
        }

        return lista;
    }

    public int getIdRecordRuta() {
        return idRecordRuta;
    }

    public void setIdRecordRuta(int idRecordRuta) {
        this.idRecordRuta = idRecordRuta;
    }

    public short getPosRecordRuta() {
        return posRecordRuta;
    }

    public void setPosRecordRuta(short posRecordRuta) {
        this.posRecordRuta = posRecordRuta;
    }

    public String getCampoBuscar() {
        return campoBuscar;
    }

    public void setCodigoRuta(String codigoRuta) {
        this.campoBuscar = codigoRuta;
    }

    public void writeXMLObject(KXmlSerializer writer) throws IOException {
    }

    public void readXMLObject(KXmlParser parser) throws IOException {
        setIdRecordRuta(Integer.parseInt(parser.getAttributeValue(null, "i")));
        setPosRecordRuta(Short.parseShort(parser.getAttributeValue(null, "p")));
        setCodigoRuta(parser.getAttributeValue(null, "r"));
    }

    //retorna negativo <0 -> this es menor al arg0
    //retorna 0 -> this = arg0
    //retorna positivo >0 -> this es mayor al arg0
    public int compareTo(Object o) {
        try {

            RutaPorCodigoBean bean = (RutaPorCodigoBean) o;

                return this.getCampoBuscar().compareTo(bean.getCampoBuscar());


        } catch (Exception e) {
            return -1;
        }
    }

    public boolean equals(Object o) {
        try {
            RutaPorCodigoBean bean = (RutaPorCodigoBean) o;
            if (this.getCampoBuscar().equals(bean.getCampoBuscar())) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public int hashCode() {
        if (getCampoBuscar() != null) {
            return getCampoBuscar().hashCode();
        } else {
            return 0;
        }
    }

    public void clear() {

    }
}
